﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SenerPro;

namespace MensUnoShop.Models
{
    public class MagazineMarket : Market,IMarket
    {
        #region private attribute
        private string _introduction;       //賣場內容
        private int _newprice;
        private int _oldprice;
        private List<MarketImage> _marketimg;
        private DBMarket _marketrepository;
        private IMarketSalesKit _marketsaleskit;
        #endregion

        public string INTRODUCTION { set { _introduction = value; } get { return _introduction; } }
        public int NEWPRICE { set { _newprice = value; } get { return _newprice; } }
        public int OLDPRICE { set { _oldprice = value; } get { return _oldprice; } }
        public List<MarketImage> MARKETIMG { set { _marketimg = value; } get { return _marketimg; } }
        public IMarketSalesKit MARKETSALESKIT { set { _marketsaleskit = value; } get { return _marketsaleskit; } }

        public MagazineMarket() { _marketrepository = new DBMarket(); } 

        
        /// <summary>
        /// 取得該賣場的折扣saleskit
        /// </summary>
        public void GetMarketSalesKit(int buyamount) 
        {
            int total = buyamount * _price;
            _marketsaleskit = _marketrepository.GetMarketSalesKit(_mkguid, buyamount, total);
        }
        


        public bool SetMarketAmount(ref OrderMarket om, Guid scguid, int num)
        {
            bool ischk = false;
            try
            {
                if (_marketrepository.SetSpecStock(scguid, om.MOKGUID,om.MKGUID, om.SPECID, num))
                {
                    om.AMOUNT = num;
                    om.PRICE = om.SINGLEPRICE * num;
                    GetMarketSalesKit(num);
                    if (_marketsaleskit != null && _marketsaleskit.TYPE == Config.SalesKitType.PriceDiscount)
                    {
                        ITradeInDiscount dsk = (ITradeInDiscount)_marketsaleskit;
                        dsk.GetDiscount();
                        om.DISCOUNT = dsk.DEPRICE;
                        om.DISCOUNTNOTE = _marketsaleskit.NOTE;
                    }
                    ischk = true;
                }
            }
            catch (Exception ex)
            {
                Logger log = new Logger(Config.logpath);
                log.RecordExceptionFormat(log.FlowLogPath, "[MagazineMarket]", scguid + "\r\n" + ex.ToString());
            }
            return ischk;
        }

        public bool CheckMarket(Guid scguid, ref List<OrderMarket> mlist, Guid[] pdguid, string[] specid, int[] amount, ref string error)
        {
            int oriamount = 0;
            bool ischeck = false;
            bool isexist = false;
            try
            {
                Guid _pdguid = pdguid[0];
                var p = _plist.First();
                p.GetSpecification();
                Guid _specid = p.SPEC.First().SID;
                int _amount = amount[0];
                //確認該賣場規格數量是否OK
                var mm = mlist.SingleOrDefault(m => m.MKGUID == _mkguid && m.SPECID == _specid);
                if (DateTime.Compare(_sdate, DateTime.Now) < 0 && DateTime.Compare(_edate, DateTime.Now) > 0 && _status)
                {
                    if (mm != null)
                    {
                        isexist = true;
                        oriamount = mm.AMOUNT;
                        _amount += mm.AMOUNT;
                        mm.AMOUNT = _amount;
                    }
                    else
                    {
                        mm = initialOrderMarket(_pdguid, _amount, _specid);
                        if (specid[0] == "0")
                            mm.SINGLEPRICE = _newprice;
                        else
                            mm.SINGLEPRICE = _oldprice;
                        mlist.Add(mm);
                    }
                    if (_marketrepository.SetSpecStock(scguid, mm.MOKGUID, mm.MKGUID, _specid, _amount))
                    {
                        mm.PRICE = mm.SINGLEPRICE * _amount;
                        //一般賣場的數量=商品的數量
                        GetMarketSalesKit(_amount);
                        if (_marketsaleskit != null && _marketsaleskit.TYPE == Config.SalesKitType.PriceDiscount)
                        {
                            ITradeInDiscount dsk = (ITradeInDiscount)_marketsaleskit;
                            dsk.GetDiscount();
                            mm.DISCOUNT = dsk.DEPRICE;
                            mm.DISCOUNTNOTE = _marketsaleskit.NOTE;
                        }

                        ischeck = true;
                    }
                    else
                    {
                        error = "本賣場暫無庫存!!";
                        //數量還原或移除新增的購物賣場
                        if (isexist)
                        {
                            mm.AMOUNT = oriamount;
                        }
                        else
                            mlist.Remove(mm);
                    }
                }
                else
                {
                    error = "本賣場尚未開賣!!";
                }
            }
            catch (Exception ex)
            {
                Logger _log = new Logger(Config.logpath);
                _log.RecordExceptionFormat(_log.DBExceptionPath, "MagaCheckMarket", ex.ToString());
                error = "賣場錯誤!!";
            }

            return ischeck;
        }
    }
}
